package com.sun.module_common.widget

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshDefaults
import androidx.compose.material3.pulltorefresh.pullToRefresh
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.widget.HorizontalDottedProgressBar
import com.sun.module_common.model.Article
import com.sun.module_network.utils.loge
import com.sun.module_network.utils.logi

/**
 *@author:sunc
 *@time :2025/8/15 16:17
 *@desc: 描述 刷新控件
 */

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RefreshScreen(
    dataList: List<Article>,
    appThemeState: MutableState<AppThemeState> = mutableStateOf(AppThemeState()),
    isRefreshing: Boolean = false,
    isLoadMore: Boolean = false,
    onRefresh: () -> Unit = {},
    onLoadMore: () -> Unit = {},
    itemClick: (Article) -> Unit,
    listTop: @Composable ()-> Unit={}
) {

    val pullRefreshState = rememberPullToRefreshState()


    val listState = rememberLazyListState()
    LaunchedEffect(listState) {
        snapshotFlow { listState.layoutInfo.visibleItemsInfo }
            .collect { visibleItems ->
                if (visibleItems.lastOrNull()?.index == dataList.size - 1 && !isLoadMore) {
                    onLoadMore.invoke()
                    "加载更多".logi()
                }
            }
    }
    Box(
        modifier = Modifier.pullToRefresh(
            isRefreshing = isRefreshing,
            state = pullRefreshState,
            onRefresh = onRefresh
        )
    ) {
        /**
         * LazyColumn的主题切换延迟问题，可通过
         * 通过key()强制在主题变化时重建LazyColumn
         */
        LazyColumn(
            modifier = Modifier
                .fillMaxSize(),
            state = listState,
            contentPadding = WindowInsets.safeDrawing.only(
                WindowInsetsSides.Horizontal + WindowInsetsSides.Top
            ).asPaddingValues(),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            item {
                listTop.invoke()
            }

            itemsIndexed(dataList, key = {index,item->item.id}) {index,item->
                key(appThemeState.value) {// 关键点：以主题色为重组触发条件
                    ArticleItem(data = item, index = index) {
                        itemClick.invoke(item)
                    }
                }
            }
            if (isLoadMore) {
//                item { CircularProgressIndicator() }

                item {
                    key(appThemeState.value) {// 关键点：以主题色为重组触发条件
                        HorizontalDottedProgressBar()
                    }
                }
            }
        }
        key(appThemeState.value) {// 关键点：以主题色为重组触发条件
            PullToRefreshDefaults.Indicator(
                state = pullRefreshState,
                color = MaterialTheme.colorScheme.primary,
                containerColor = MaterialTheme.colorScheme.background,
                isRefreshing = isRefreshing, modifier = Modifier.align(
                    Alignment.TopCenter
                )
            )
        }

    }
}